home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / snip9503 / commconv.c < prev    next >
C/C++ Source or Header  |  1995-03-14  |  4KB  |  138 lines

  1. /*
  2.  * COMMCONV.C
  3.  * Change C++ comments to C comments
  4.  *
  5.  * ver 2.0, 05 Mar 1995
  6.  *
  7.  * Public domain by:
  8.  *   Jari Laaksonen
  9.  *   Arkkitehdinkatu 30 A 2
  10.  *   FIN-33720 Tampere
  11.  *   FINLAND
  12.  *
  13.  *   Fidonet : 2:221/360.20
  14.  *   Internet: jla@to.icl.fi
  15.  */
  16.  
  17. #include <stdio.h>
  18.  
  19. int main (int argc, char **argv)
  20. {
  21.       int   Char,
  22.             Char2,
  23.             cpp_comment   = 0,
  24.             c_comment     = 0,
  25.             in_string     = 0,
  26.             cpp_multiline = 0;
  27.       char CannotOpen[] = "Cannot open %s\n\n";
  28.       FILE *InFile, *OutFile = stdout;
  29.  
  30.       if (argc < 2)
  31.       {
  32.             fprintf (stderr, "USAGE: COMMCONV InFile [OutFile]\n");
  33.             return 1;
  34.       }
  35.       if ((InFile = fopen (argv[1], "r")) == NULL)
  36.       {
  37.             fprintf (stderr, CannotOpen, argv[1]);
  38.             return 2;
  39.       }
  40.  
  41.       if (argc == 3)
  42.       {
  43.             if ((OutFile = fopen (argv[2], "w")) == NULL)
  44.             {
  45.                   fprintf (stderr, CannotOpen, argv[2]);
  46.                   /* if can't open, output goes to stdout instead */
  47.                   OutFile = stdout;
  48.             }
  49.       }
  50.  
  51.       while ((Char = fgetc (InFile)) != EOF)
  52.       {
  53.             fputc (Char, OutFile);
  54.  
  55.             if (Char == '\"')
  56.             {
  57.                   Char2 = fgetc (InFile);       /* check next char      */
  58.                   if (Char2 != '\'')            /* character constant?  */
  59.                         in_string = ! in_string;/* no, toggle flag      */
  60.                   fputc (Char2, OutFile);
  61.             }
  62.  
  63.             if (in_string)                /* we are in a string now     */
  64.                   continue;
  65.  
  66.             if (Char == '/')
  67.             {
  68.                   Char = fgetc (InFile);  /* check next char            */
  69.                   if (Char == '/')        /* is it start of C++ comment */
  70.                   {
  71.                         Char = '*';       /* change it to C comment     */
  72.                         cpp_comment = 1;
  73.                   }
  74.                   else if (Char == '*')   /* is it start of C comment   */
  75.                         c_comment = 1;
  76.  
  77.                   fputc (Char, OutFile);
  78.             }
  79.             else if (Char == '*' && c_comment)
  80.             {
  81.                   Char = fgetc (InFile);
  82.                   if (Char == '/')        /* is it end of C comment     */
  83.                         c_comment = 0;
  84.                   fputc (Char, OutFile);
  85.             }
  86.  
  87.             if (c_comment || cpp_comment) /* inside C or C++ comment    */
  88.             {
  89.                   /* check the rest of the line       */
  90.  
  91.                   while ((Char = fgetc (InFile)) != '\n')
  92.                   {
  93.                         if (Char == '\\' && cpp_comment)
  94.                               cpp_multiline = 1;
  95.                         if (Char == '*')
  96.                         {
  97.                               Char2 = fgetc (InFile); /* check next char   */
  98.                               ungetc (Char2, InFile); /* put it back       */
  99.                               if (Char2 == '/')       /* end of C comment? */
  100.                               {
  101.                                     c_comment = 0;
  102.                                     /* end of C comment inside C++ comment?*/
  103.                                     if (cpp_comment)
  104.                                     {
  105.                                           fputs ("* ", OutFile);
  106.                                           Char = fgetc (InFile);
  107.                                     }
  108.                               }
  109.                         }
  110.                         fputc (Char, OutFile);
  111.                   }
  112.                   if (cpp_comment && cpp_multiline == 0)
  113.                   {
  114.                         /* put ending C comment mark */
  115.                         fputs (" */", OutFile);
  116.                         cpp_comment = 0;
  117.                   }
  118.                   fputc ('\n', OutFile);
  119.  
  120.                   /* clear flag for the next round. if it is still clear after
  121.                      next C++ comment line is processed, multiline C++ comment
  122.                      is ended.
  123.                   */
  124.  
  125.                   cpp_multiline = 0;
  126.             }
  127.       } /* while end */
  128.  
  129.       if (argc == 3)
  130.             fclose (OutFile);
  131.       fclose (InFile);
  132.  
  133.       fflush (stdout);
  134.       fprintf (stderr, "\nOK!\n");
  135.  
  136.       return 0;
  137. }
  138.